from framework.basecontroller import BaseHandler
from framework.result import Result
from games import *
from games.basegame import activeGames
from games.player import Player

class HomeHandler(BaseHandler):
	def get(self):
		if 'TicTacToe' in activeGames:
			self.model['TicTacToe'] = activeGames['TicTacToe']
		self.renderTemplate('lounge.html')

class CreateNewGameHandler(BaseHandler):
	def post(self, gameType):
		newGame = {
			'TicTacToe': TicTacToe
		}[gameType]()
		raise self.redirect('/')

class JoinGameHandler(BaseHandler):
	def get(self, gameType, id):
		requestedGame = activeGames[gameType][id]
		requestedGame.addPlayer(Player(self.model['id'], self.model['nickname']))
		self.model['game'] = requestedGame
		userId = self.user.user_id()
		if userId in requestedGame.players:
			self.model['token'] = requestedGame.players[userId].channel
		self.renderTemplate('game.html')

class MoveGameHandler(BaseHandler):
	def post(self, gameType, id):
		requestedGame = activeGames[gameType][id]
		requestedGame.move(self.user.user_id(), self.request.POST['move'])

class RefreshGameHandler(BaseHandler):
	def get(self, gameType, id):
		self.model['game'] = activeGames[gameType][id]
		self.renderTemplate('ticTacToe.html')
